﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//using GalaxyWars_Server;
using System.Diagnostics;

namespace Galaxy_Wars
{
    partial class NewGame : Form
    {
        string ip;
        string gameType;
        public NewGame()
        {
            InitializeComponent();
        }

        private void Btn_Connect_Click(object sender, EventArgs e)
        {
            if (checkAllFields())
            {
                //because you are the host you will also need the galaxy wars server to start.
                Process.Start("GalaxyWars_Server.exe");

                Program.startGame(Tbox_Username.Text, Convert.ToInt32(SelectAmountPlanets.Value),
                    Convert.ToInt32(SelectShipsPerTurn.Value), Convert.ToInt32(SelectMinutesTurn.Value), Tbox_IPAdres.Text, "host");
                
                //this.Hide();
                //this.Dispose();
                this.Close();
                //return;
            }
        }

        private Boolean checkAllFields()
        {
            Tbox_Username.BackColor = Color.White;
            Tbox_IPAdres.BackColor = Color.White;

            gameType = tabC_HostJoin.SelectedTab.Text;
            Debug.WriteLine("Selected Tab: " + gameType);

            if (Tbox_Username.Text != "" && SelectAmountPlanets.Value >= 5 && SelectShipsPerTurn.Value > 0 && SelectMinutesTurn.Value > 0 && Tbox_IPAdres.Text != "")
            {
                return true;
            }
            else 
            {
                if (Tbox_Username.Text == "")
                {
                    Tbox_Username.BackColor = Color.Red;
                    Tbox_Username.Text = "Mordechai";
                }
                if (Tbox_IPAdres.Text == "")
                {
                    Tbox_IPAdres.BackColor = Color.Red;
                    Tbox_IPAdres.Text = "127.0.0.1";
                }

                return false;
            }
        }

        public Boolean CheckJoinFields() {
            
            gameType = tabC_HostJoin.SelectedTab.Text;
            Debug.WriteLine("Selected Tab: " + gameType);

            if (Tbox_NameJoin.Text != "" && Tbox_joinipadres.Text != "" && gameType != "") {
                return true;
            }

            else
            {
                if (Tbox_NameJoin.Text == "")
                {
                    Tbox_NameJoin.BackColor = Color.Red;
                    Tbox_NameJoin.Text = "1";
                }
                if (Tbox_joinipadres.Text == "")
                {
                    Tbox_joinipadres.BackColor = Color.Red;
                    Tbox_joinipadres.Text = "127.0.0.1";
                }

            }
            
            return false;
        }

        private void Btn_Join_Click(object sender, EventArgs e)
        {
            if (CheckJoinFields())
            {
                Program.startJoinGame(Tbox_NameJoin.Text, Tbox_joinipadres.Text, "join");
                Debug.WriteLine("Join = Okido");
            }
            else { Debug.WriteLine("Join = Hell no"); }
        }

        //public ProcessStartInfo GalaxyWars_Server { get; set; }
    }
}
